<?php

namespace app\common\model\banxi\grammar;

use app\common\enums\CategoryStateEnum;
use app\common\model\common\BaseModel;
use think\exception\ValidateException;
use think\model\concern\SoftDelete;

/**
 * 课程字典
 */
class GrammarCategory extends BaseModel
{
    use SoftDelete;

    protected $name = 'banxi_grammar_category';

    protected $autoWriteTimestamp = true;

    protected $deleteTime = 'del';

    protected $defaultSoftDelete = 0;

    protected $append = [
        'stateCn'
    ];

    protected $type = [
        'id' => 'string',
        'del' => 'string',
        'gradeId' => 'string',
        'parentId' => 'string',
        'createTime' => 'timestamp:Y-m-d H:i:s',
        'updateTime' => 'timestamp:Y-m-d H:i:s',
    ];

    //-----------------------------------------------------------------------------------
    //｜                                   获取器                                        ｜
    //-----------------------------------------------------------------------------------

    public function getStateCnAttr($value, $data)
    {
        $value = $value ?: ($data['state'] ?? '');
        return (new CategoryStateEnum())->getDescription($value) ?? '';
    }

    //-----------------------------------------------------------------------------------
    //｜                                   处理方法                                       ｜
    //-----------------------------------------------------------------------------------

    public function getListPage(array $where, string $page = "1", int $limit = 10)
    {
        return $this->with(['gradeInfo'])->where($where)
            ->paginate([
                'page' => $page,
                'list_rows' => $limit,
            ]);
    }

    public function getInfo(array $where)
    {
        return $this->with(['gradeInfo'])->where($where)
            ->find();
    }

    public function saveOrUpdate(array $data)
    {
        $parentId = $data['parentId'];
        if ($parentId == 0) {
            $data['level'] = 0;
        } else {
            $category = $this->where(['id' => $parentId])->field('level,gradeId')->find();
            if (!$category) {
                throw new ValidateException('上级分类不存在');
            }
            $data['level'] = bcadd($category['level'], 1, 0);
            $data['gradeId'] = $category['gradeId'];
        }
        $id = $data['id'] ?? 0;
        if (!$id) {
            $this->save($data);
        } else {
            $info = $this->where(['id' => $id])->find();
            if (!$info) {
                throw new ValidateException('记录不存在');
            }
            $info->save($data);
        }
        return true;
    }

    public function del(array $ids)
    {
        self::destroy(function ($query) use ($ids) {
            $query->whereIn('id|parentId', $ids);
        });
    }

    //-----------------------------------------------------------------------------------
    //｜                                   模型关联                                       ｜
    //-----------------------------------------------------------------------------------

    public function gradeInfo()
    {
        return $this->hasOne(GrammarGrade::class, 'id', 'gradeId');
    }
}